<style lang="less">
@import "@/styles/table-common.less";
.btn-box {
  // padding: 10px 12px 10px 0;
  height: 32px;
  display: flex;
  align-items: center;
  span {
    margin-left: 15px;
  }
}
.detail-modal {
  .form {
    div {
      display: flex;
      margin-bottom: 10px;
      span {
        width: 130px;
      }
      input {
        flex: 1;
        border: 1px solid #e8eaec;
        outline: none;
        text-indent: 16px;
        color: #515a6e;
      }
    }
  }
  .refund-num {
    margin-top: 15px;
  }
}
</style> 
<template>
  <div class="search">
    <Card>
      <Row class="operation">
        <Button @click="add" type="primary" icon="md-add">添加</Button>
      </Row>
      <Form ref="searchForm" :model="searchForm" inline :label-width="100">
        <Row v-show="openSearch" @keydown.enter.native="handleSearch">
          <FormItem label="标题：">
            <Input
              type="text"
              v-model="searchForm.title"
              clearable
              placeholder="请输入通知标题"
              style="width: 200px"
            />
          </FormItem>
          <FormItem label="状态选择：">
            <Select
              v-model="searchForm.status"
              transfer
              placeholder="请选择"
              style="width: 200px"
            >
              <Option
                v-for="item in StatusList"
                :value="item.value"
                :key="item"
                >{{ item.title }}</Option
              >
            </Select>
          </FormItem>
          <FormItem style="margin-left: -35px" class="br">
            <Button @click="handleSearch" type="primary" icon="ios-search"
              >搜索</Button
            >
            <Button @click="handleReset">重置</Button>
          </FormItem>
        </Row>
      </Form>

      <Table
        :loading="loading"
        border
        :columns="columns"
        :data="data"
        sortable="custom"
        ref="table"
      >
        <template slot-scope="{ row }" slot="validTime">
          <div v-if="row.siteValidTimes">
            <div v-for="item in row.siteValidTimes" :key="item.id">
              {{ item.delFlag }}
            </div>
          </div>
        </template>
        <template slot-scope="{ row }" slot="operation">
          <!-- <a @click="chageable(row, '-2')">作废</a>
           <Divider type="vertical" /> -->
          <a @click="chageable(row, '0')" v-if="row.status==-1">应用</a>
           <a @click="chageable(row, '-1')" v-if="row.status==0">取消应用</a>
        </template>
      </Table>
      <Row type="flex" justify="end" class="page">
        <Page
          :current="searchForm.pageNumber"
          :total="total"
          :page-size="searchForm.pageSize"
          @on-change="changePage"
          @on-page-size-change="changePageSize"
          :page-size-opts="[10, 20, 50]"
          size="small"
          show-total
          show-elevator
          show-sizer
        ></Page>
      </Row>
    </Card>
    <addEdit
      :formData="currentobj"
      v-model="showUser"
      v-if="showUser"
      @on-submit="setResult"
    />
  </div>
</template>

<script>
import {
  scNoticeupdStatusById,
  scNoticeeffect,
  scNoticeByCondition,
 
} from "@/api/index";
import addEdit from "./addEdit.vue";
export default {
  name: "user-manage",
  components: {
    addEdit,
  },
  data() {
    return {
      showUser: false,
      currentobj: {},
      loading: true,
      selectDate: "",
      openSearch: true,
      StatusList: [
        { value: "0", title: "应用中" },
        { value: "-1", title: "未应用" },
      ],
      searchForm: {
        pageNumber: 1,
        pageSize: "10",
        sort: "createTime",
        order: "desc",
      },
      columns: [
        {
          title: "ID",
          key: "id",
          align: "center",
          fixed: "left",
        },
        {
          title: "通知标题",
          key: "title",

          align: "center",
        },
          {
          title: "备注",
          key: "remark",

          align: "center",
        },

        {
          title: "创建时间",
          key: "createTime",
          align: "center",
        },
        {
          title: "启用状态",
          key: "status",
          width: 140,
          align: "center",
          render: (h, params) => {
            if (params.row.status == 0) {
              return h("div", [
                h("Badge", {
                  props: {
                    status: "success",
                    text: "已启用",
                  },
                }),
              ]);
            } else if (params.row.status == -1) {
              return h("div", [
                h("Badge", {
                  props: {
                    status: "error",
                    text: "未启用",
                  },
                }),
              ]);
            }
          },
          filterRemote: (e) => {
            let v = "";
            if (e.length > 0) {
              v = e[0];
            }
            this.searchForm.status = v;
            this.searchForm.pageNumber = 1;
            this.getDataList();
          },
        },
        {
          title: "操作",
          key: "createTime",
          width: 150,
          align: "center",
          fixed: "right",
          slot: "operation",
        },
      ],
      modal: false,
      data: [],
      formData: {},
    };
  },
  methods: {
    setResult(val) {
      console.log("vl:", val);
      this.showUser = false;
      if (val) {
        //需要更新数据
        this.getDataList();
      }
    },
    init() {
      this.getDataList();
    },
    changeSort(e) {
      this.searchForm.sort = e.key;
      this.searchForm.order = e.order;
      if (e.order == "normal") {
        this.searchForm.order = "";
      }
      this.getDataList();
    },
    getDataList() {
      // 多条件搜索用户列表
      this.loading = true;
      scNoticeByCondition(this.searchForm).then((res) => {
        this.loading = false;
        if (res.success) {
          this.data = res.result.content;
          this.total = res.result.totalElements;
          if (this.data.length == 0 && this.searchForm.pageNumber > 1) {
            this.searchForm.pageNumber -= 1;
            this.getDataList();
          }
        }
      });
    },

    add() {
      this.currentobj = {};
      this.showUser = true;
    },
    edit(item) {
      // console.log('seeting')
      this.currentobj = item;
      this.showUser = true;
    },
    changePage(v) {
      this.searchForm.pageNumber = v;
      this.getDataList();
    },
    changePageSize(v) {
      this.searchForm.pageSize = v;
      this.getDataList();
    },
    selectDateRange(v) {
      if (v) {
        this.searchForm.startDate = v[0];
        this.searchForm.endDate = v[1];
      }
    },
    handleSearch() {
      this.searchForm.pageNumber = 1;
      this.searchForm.pageSize = 10;
      this.getDataList();
    },
    handleReset() {
      this.selectDate = "";
      this.searchForm = {
        pageNumber: 1,
        pageSize: 10,
      };
      // 重新加载数据
      this.getDataList();
    },

    chageable(v, status) {
      //  status:-1:作废 0:正常 1:即时启动
      let statusTxt;
      if (status == -2) {
        statusTxt = "作废";
      }
      if (status == 0) {
        statusTxt = "应用";
      }
      if(status == -1){
          statusTxt = '取消应用'
      }
      this.$Modal.confirm({
        title: `确认${statusTxt}`,
        content: `您确认要${statusTxt}` + v.title + " ?",
        loading: true,
        onOk: () => {
          if (status == -1 || status == 0) {
            scNoticeupdStatusById({ ...v, status }).then((res) => {
              this.$Modal.remove();
              if (res.success) {
                this.$Message.success("操作成功");
                this.getDataList();
              }
            });
          } else if (status == -2) {
            scNoticeeffect({ ...v, status }).then((res) => {
              this.$Modal.remove();
              if (res.success) {
                this.$Message.success("操作成功");
                this.getDataList();
              }
            });
          }
        },
      });
    },
    cancel() {},
    ok() {},
  },
  created() {},
  mounted() {
    // 计算高度
    this.init();
  },
};
</script>